package com.newlink.building.main.ui

import android.graphics.Bitmap
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.view.animation.Animation
import android.view.animation.LinearInterpolator
import android.view.animation.ScaleAnimation
import ceneax.app.lib.qrscan.QRScan
import ceneax.app.lib.qrscan.arch.IScanCallback
import ceneax.app.lib.qrscan.bean.ParseResult
import ceneax.app.lib.qrscan.widget.MultiResultsView
import com.newlink.building.common_base.base.NL_BaseActivity
import com.newlink.building.common_base.ext.showToast
import com.newlink.building.main.R
import com.newlink.building.main.databinding.ActivityQrBindBinding
import org.jetbrains.anko.toast

/**
 * Created by xiaodong on 2022/10/2.
 */
class Module_Main_QRBindActivityNL : NL_BaseActivity(), IScanCallback {
    private var mQRScan: QRScan? = null

    //    override fun attachLayoutRes(): Int  = R.layout.activity_qr_bind
    private lateinit var mBinding: ActivityQrBindBinding
    override fun attachLayoutRes(): View {
        mBinding = ActivityQrBindBinding.inflate(layoutInflater)
        return mBinding.root
    }


    override fun initData() {
    }

    override fun initView() {
        initColor()
        mQRScan = QRScan.Builder(this)
            .setQRPreviewView(findViewById(R.id.qrPreviewView))
            .setScanCallback(this)
            .build()
        mBinding.scanLight.setOnClickListener(View.OnClickListener { v: View? ->
            if (mQRScan!!.qrCamera.isTorchEnabled) {
                mQRScan!!.qrCamera.enableTorch(false)
                mBinding.scanLight.setImageResource(R.drawable.ceneax_app_lib_qrscan_icon_torch_disable)
            } else {
                mQRScan!!.qrCamera.enableTorch(true)
                mBinding.scanLight.setImageResource(R.drawable.ceneax_app_lib_qrscan_icon_torch_enable)
            }
        })
        scanAnimation(mBinding.captureScanLine)

        mBinding.topBarBack.setOnClickListener {
            finish()
        }

        mBinding.scanManualBind.setOnClickListener {
            showToast(resources.getString(R.string.tip_device_add_failed))
        }
    }

    private fun scanAnimation(view: View) {
        val animation = ScaleAnimation(1.0f, 1.0f, 0.0f, 1.0f)
        animation.repeatCount = -1
        animation.repeatMode = Animation.RESTART
        animation.interpolator = LinearInterpolator()
        animation.duration = 1200
        view.startAnimation(animation)
    }

    override fun initColor() {
        initColor(R.color.black)
    }

    override fun onResult(bitmap: Bitmap, results: Array<out ParseResult>) {
        if (results.size == 1) {
            bitmap.recycle()
            finishWithResult(results[0])
            return
        }
        val drawView = MultiResultsView(this, bitmap, *results)
        drawView.setBackgroundColor(Color.BLACK)
        drawView.tag = "multi_results_container"
        drawView.setOnSelectedListener { result: ParseResult? ->
            result?.let {
                this.finishWithResult(
                    result
                )
            }
        }
        mBinding.captureContainter.addView(drawView)

        mQRScan!!.qrCamera.unBindAll()
    }

    private fun finishWithResult(result: ParseResult) {
        toast("扫码信息为:${result.content}")
        val bundle = Bundle()
        bundle.putString("data", result.content)
        setResult(RESULT_OK, intent.putExtras(bundle))
        finish()
    }
}